import http from '../utils/httpRequest.js'

let goodsCategoryListTest = [
    {"id": 1, name: "testCategory1", "displayOrder": 1, showStatus: true},
    {"id": 2, name: "testCategory2", "displayOrder": 2, showStatus: false},
    {"id": 3, name: "testCategory3", "displayOrder": 3, showStatus: true},
    {"id": 4, name: "testCategory4", "displayOrder": 40, showStatus: true},
    {"id": 5, name: "testCategory5", "displayOrder": 50, showStatus: true},
]

// 所有商品类别
export function getGoodsCategoryList(params) {
    http({
        ...params,
        path: "/goodsCategory/list",
        testData: JSON.parse(JSON.stringify(goodsCategoryListTest)), // 触发视图更新
        showLoading: false,
    })
}

// 添加
export function addGoodsCategory(params) {
    http({
        ...params,
        method: "post",
        path: "/goodsCategory",
        testData: function () {
            goodsCategoryListTest.push({
                ...params.data,
                id: Math.random()
            })
        },
    })
}

// 更新
export function updateGoodsCategory(params) {
    http({
        ...params,
        method: "put",
        path: "/goodsCategory/" + params.data.id,
        testData: function () {
            for (let i = 0; i < goodsCategoryListTest.length; i++)
                if (goodsCategoryListTest[i].id === params.data.id)
                    goodsCategoryListTest[i] = params.data
            return true
        },
    })
}

// 删除
export function deleteGoodsCategory(params) {
    http({
        ...params,
        method: "delete",
        path: "/goodsCategory/" + params.id,
        testData: function () {
            for (let i = 0; i < goodsCategoryListTest.length; i++)
                if (goodsCategoryListTest[i].id === params.id)
                    goodsCategoryListTest.splice(i, 1)
            return true
        },
    })
}
